package realphone

import (
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `real_phone_template` (
//  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '账号ID',
//  `brand` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '品牌',
//  `model` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '型号',
//  `fingerprint_md5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '安卓md5(ro.build.fingerprint)',
//  `fingerprint` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '安卓ro.build.fingerprint',
//  `resource_specification_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '规格编号',
//  `screen_layout_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '屏幕布局编码',
//  `adi_template_download_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'adi模板文件下载路径',
//  `adi_template_pwd` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'adi模板文件密码',
//  `property_json` json DEFAULT NULL COMMENT '实例属性',
//  `android_image_version` int DEFAULT '0' COMMENT '安卓镜像版本',
//  `delete_flag` tinyint DEFAULT '0' COMMENT '是否删除（1：已删除；0：未删除）',
//  `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建人',
//  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
//  `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '修改人',
//  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
//  `device_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '机型名称(别名/展示用)',
//  `is_official` tinyint DEFAULT '0' COMMENT '是否正式版（0:测试版 1:正式版）',
//  `status` tinyint DEFAULT '0' COMMENT '是否启用（0:禁用 1:启用）',
//  `adi_template_version` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'adi模板版本号',
//  `model_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '机型标识',
//  `is_public` tinyint DEFAULT '1' COMMENT '模板使用类型(1=公共 0=私有)',
//  `aosp_version` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'aosp版本',
//  `test_cases_download_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '测试用例文件下载路径',
//  PRIMARY KEY (`id`) USING BTREE,
//  KEY `idx_is_public_create_time` (`is_public`,`create_time`) USING BTREE,
//  KEY `index_is_public_create_time` (`is_public`,`create_time`) USING BTREE
//) ENGINE=InnoDB AUTO_INCREMENT=10493 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='云真机模板文件表';

type RealPhoneTemplate struct {
	Id                        int64     `json:"id" gorm:"primary_key;column:id"`
	Brand                     string    `json:"brand" gorm:"column:brand"`
	Model                     string    `json:"model" gorm:"column:model"`
	FingerprintMd5            string    `json:"fingerprint_md5" gorm:"column:fingerprint_md5"`
	Fingerprint               string    `json:"fingerprint" gorm:"column:fingerprint"`
	ResourceSpecificationCode string    `json:"resource_specification_code" gorm:"column:resource_specification_code"`
	ScreenLayoutCode          string    `json:"screen_layout_code" gorm:"column:screen_layout_code"`
	AdiTemplateDownloadUrl    string    `json:"adi_template_download_url" gorm:"column:adi_template_download_url"`
	AdiTemplatePwd            string    `json:"adi_template_pwd" gorm:"column:adi_template_pwd"`
	PropertyJson              string    `json:"property_json" gorm:"column:property_json"`
	AndroidImageVersion       int       `json:"android_image_version" gorm:"column:android_image_version"`
	DeleteFlag                int       `json:"delete_flag" gorm:"column:delete_flag"`
	CreateBy                  string    `json:"create_by" gorm:"column:create_by"`
	CreateTime                time.Time `json:"create_time" gorm:"column:create_time"`

	UpdateBy             string    `json:"update_by" gorm:"column:update_by"`
	UpdateTime           time.Time `json:"update_time" gorm:"column:update_time"`
	DeviceName           string    `json:"device_name" gorm:"column:device_name"`
	IsOfficial           int       `json:"is_official" gorm:"column:is_official"`
	Status               int       `json:"status" gorm:"column:status"`
	AdiTemplateVersion   string    `json:"adi_template_version" gorm:"column:adi_template_version"`
	ModelCode            string    `json:"model_code" gorm:"column:model_code"`
	IsPublic             int       `json:"is_public" gorm:"column:is_public"`
	AospVersion          string    `json:"aosp_version" gorm:"column:aosp_version"`
	TestCasesDownloadUrl string    `json:"test_cases_download_url" gorm:"column:test_cases_download_url"`
}

func (a *RealPhoneTemplate) TableName() string {
	return "real_phone_template"
}

func (a *RealPhoneTemplate) Create(db *gorm.DB) error {
	return db.Create(a).Error
}

func (a *RealPhoneTemplate) Update(db *gorm.DB) error {
	return db.Update(a).Error
}

func (a *RealPhoneTemplate) GetById(db *gorm.DB) error {
	return db.Where("id = ?", a.Id).First(a).Error
}
func (a *RealPhoneTemplate) GetByIdForUpdate(db *gorm.DB) error {
	return db.Where("id = ?", a.Id).First(a).Update("id", a.Id).Error
}
func (a *RealPhoneTemplate) Getlist(db *gorm.DB, offset int, limit int) ([]RealPhoneTemplate, int, error) {
	var list []RealPhoneTemplate
	var total int
	err := db.Offset(offset).Limit(limit).Find(&list).Error
	if err != nil {
		return nil, 0, err
	}
	err = db.Model(&RealPhoneTemplate{}).Count(&total).Error
	if err != nil {
		return nil, 0, err
	}
	return list, total, nil
}
func (a *RealPhoneTemplate) GetlistByBrand(db *gorm.DB, brand string, offset int, limit int) ([]RealPhoneTemplate, int, error) {
	var list []RealPhoneTemplate
	var total int
	err := db.Where("brand = ?", brand).Offset(offset).Limit(limit).Find(&list).Error
	if err != nil {
		return nil, 0, err
	}
	err = db.Model(&RealPhoneTemplate{}).Where("brand = ?", brand).Count(&total).Error
	if err != nil {
		return nil, 0, err
	}
	return list, total, nil
}
func (a *RealPhoneTemplate) GetlistByModel(db *gorm.DB, model string, offset int, limit int) ([]RealPhoneTemplate, int, error) {
	var list []RealPhoneTemplate
	var total int
	err := db.Where("model = ?", model).Offset(offset).Limit(limit).Find(&list).Error
	if err != nil {
		return nil, 0, err
	}
	err = db.Model(&RealPhoneTemplate{}).Where("model = ?", model).Count(&total).Error
	if err != nil {
		return nil, 0, err
	}
	return list, total, nil
}

func (a *RealPhoneTemplate) GetListByAndroidImageVersion(db *gorm.DB, androidImageVersion int, offset int, limit int) ([]RealPhoneTemplate, int, error) {
	var list []RealPhoneTemplate
	var total int
	obj := db.Where("android_image_version = ?", androidImageVersion)
	if offset > 0 {
		obj = obj.Offset(offset)
	}
	if limit > 0 {
		obj = obj.Limit(limit)
	}

	err := obj.Find(&list).Error
	if err != nil {
		return nil, 0, err
	}
	err = db.Model(&RealPhoneTemplate{}).Where("android_image_version = ?", androidImageVersion).Count(&total).Error
	if err != nil {
		return nil, 0, err
	}
	return list, total, nil
}
func (a *RealPhoneTemplate) GetRandButNotOldOneByAndroidVersion(db *gorm.DB, androidVersion int, old_id int) error {
	return db.Where("android_image_version = ?", androidVersion).Where("id != ?", old_id).Order("RAND()").First(a).Error
}
